package com.ext.replacedemo;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 优先级：局部替换-->忽略的行-->整行替换
 * contains匹配到key后，通过 = 赋值，将整行替换成value,value中的{0}可以被替换为原始行，代码会自动在尾部添加被处理过的标记
 * 如果替换后的行还包含原Key，但和value一致，是不会被二次替换的。如果不一致，为了不被二次替换，记得添加完成标记。
 */
//完成标记:让本行不再被处理: /*ok*/
public class JavaXml_ReplaceLine {
    public ArrayList<String> mJXKeyLineList = new ArrayList<>();
    public ArrayList<String> mJXValueLineList = new ArrayList<>();

    private LinkedHashMap<String, String> mJXLineHashMap = new LinkedHashMap<>();

    public JavaXml_ReplaceLine(boolean migrateTo34, boolean migrateToX, boolean removeFacebook) {/*优先级低于局部替换。由于是整行替换，需手动添加空格对齐 */
        if (migrateTo34) {
            load34();
        }
        if (migrateToX) {
            loadAndroidX(removeFacebook);
        }

        if (mJXLineHashMap.size() > 0) {
            Set<String> keySet = mJXLineHashMap.keySet();
            Collection<String> values = mJXLineHashMap.values();
            mJXKeyLineList.addAll(Arrays.asList(keySet.toArray(new String[]{})));
            mJXValueLineList.addAll(Arrays.asList(values.toArray(new String[]{})));
        }
    }

    private void load34() {
        mJXLineHashMap.put("import android.content.BroadcastReceiver;", "import android.os.Build;\n{0}");
        mJXLineHashMap.put("registerReceiver(", "        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\n    {0}//删--,Context.RECEIVER_EXPORTED);/*ok*/\n        } else {\n    {0}/*ok*/\n        }");

        //        mJXLineHashMap.put("", "");
    }

    private void loadAndroidX(boolean removeFacebook) {
        /* 移除 */
        mJXLineHashMap.put("MiPushClient", "");
        /*umeng*/
        mJXLineHashMap.put("import com.umeng.", "//{0}");
        mJXLineHashMap.put("UMConfigure.", "");
        mJXLineHashMap.put("MobclickAgent.", "");
        mJXLineHashMap.put("uri = uri.buildUpon().appendQueryParameter(\"Language\", lLanguage).build();", "{0}qwerqwer");
        mJXLineHashMap.put("String lNumber = uri.getQueryParameter(AppPageUrls.Common.Call.PARAMETER_number);", "{0}\n                if (TextUtils.isEmpty(lTitle) && TextUtils.isEmpty(lMessage)) {lMessage = lNumber;}");
        mJXLineHashMap.put("    public static void startQrScanner(", "{0}/*ok*/\n" +
                "                   MLKitScanner.start(activity).subscribe(new Action1<Result<Activity>>() {\n" +
                "                        @Override\n" +
                "                        public void call(Result<Activity> result) {\n" +
                "                            switch (result.resultCode()) {\n" +
                "                                case Activity.RESULT_OK:\n" +
                "                                    String test=result.data().getStringExtra(\"test\");\n" +
                "                                    L.e(\"--->\",\"test:\"+test);\n" +
                "                                    start(activity, test);\n" +
                "                                    break;\n" +
                "                                default:\n" +
                "                                    break;\n" +
                "                            }\n" +
                "                        }\n" +
                "                    }, new Action1<Throwable>() {\n" +
                "                        @Override\n" +
                "                        public void call(Throwable throwable) {\n" +
                "                            throwable.printStackTrace();\n" +
                "                        }\n" +
                "                    });" +
                "待处理！");
        mJXLineHashMap.put("uriString.contains(\"common/qrScannerNeedCallback\")", "{0}/*ok*/\n" +
                "                        final Handler handler = new Handler();\n" +
                "                        DoVisitVJ(35);\n" +
                "                        MLKitScanner.start(AnnounceDetailActivity.this).subscribe(\n" +
                "                                new Action1<Result<Activity>>() {\n" +
                "                                    @Override\n" +
                "                                    public void call(Result<Activity> result) {\n" +
                "                                        switch (result.resultCode()) {\n" +
                "                                            case Activity.RESULT_OK:\n" +
                "                                                handler.post(new Runnable() {\n" +
                "                                                    @Override\n" +
                "                                                    public void run() {\n" +
                "                                                        String str = \"\";\n" +
                "                                                        try {\n" +
                "                                                            String test = result.data().getStringExtra(\"test\");\n" +
                "                                                            L.e(\"--->\", \"test:\" + test);\n" +
                "                                                            JSONObject object = new JSONObject();\n" +
                "                                                            object.put(\"ScanResult\", test);\n" +
                "                                                            str = object.toString();\n" +
                "                                                        } catch (JSONException e) {\n" +
                "                                                            e.printStackTrace();\n" +
                "                                                        }\n" +
                "                                                        String jsCode = \"javascript:GetqrScannerCallback(\" + str + \")\";\n" +
                "                                                        mWebView.getWebView().loadUrl(jsCode);\n" +
                "                                                    }\n" +
                "                                                });\n" +
                "                                                break;\n" +
                "                                            default:\n" +
                "                                                break;\n" +
                "                                        }\n" +
                "                                    }\n" +
                "                                }, new Action1<Throwable>() {\n" +
                "                                    @Override\n" +
                "                                    public void call(Throwable throwable) {\n" +
                "                                        throwable.printStackTrace();\n" +
                "                                    }\n" +
                "                                });待处理！");

        if (removeFacebook) {
            mJXLineHashMap.put("import com.facebook.", "//{0}");
            mJXLineHashMap.put("initFacebookShare();", "//{0}");
            mJXLineHashMap.put("FacebookSdk.sdkInitialize", "//{0}");
        }
        /*改导航栏颜色*/
        mJXLineHashMap.put(".setTitleTextColor(", "        Context context0 = AppBaseApplication.getContext();" +
                "\n        mToolbar.setBackgroundColor(SkinCompatResources.getColor(context0, R.color.titleBarBackground));" +
                "\n{0}");
        mJXLineHashMap.put(".setNavigationOnClickListener(", "        Context context0 = AppBaseApplication.getContext();" +
                "\n        mToolbar.setBackgroundColor(SkinCompatResources.getColor(context0, R.color.titleBarBackground));" +
                "\n{0}");
        mJXLineHashMap.put("import androidx.appcompat.widget.Toolbar;",
                "\nimport com.cardapp.basic.AppBaseApplication;" +
                        "\nimport skin.support.content.res.SkinCompatResources;\n{0}");
        /*分享*/
        mJXLineHashMap.put("private ShareDialog mShareDialog;", "");
        mJXLineHashMap.put("private CallbackManager mCallbackManager;", "");
        mJXLineHashMap.put("mShareDialog = shareDialog;", "");
        mJXLineHashMap.put("mCallbackManager.onActivityResult", "");
        mJXLineHashMap.put("import com.lvrenyang.cardapp.bt_printer.ChooseBtDeviceActivity;", "");

        mJXLineHashMap.put("case ANNOUNCE_PUSH:", "            case  ANNOUNCE_PUSH::\n" +
                "/*              String lUriString1 = AppPageUrls.UniversalAnnounce.AnnounceDetail.newAppLocalInstance(lMsgId);\n" +
                "                mAppPageStarterPresenter.startAppPage(lUriString1); */");

        mJXLineHashMap.put(".updatePdfUi(", "{0}//删--, System.currentTimeMillis() + \"_pdf\");");
        /* JPush */
        mJXLineHashMap.put("\"jiguang_registrationID\",\":\"", "L.w(\"JIGUANG-REG_ID\", \"registrationID:\" + JPushInterface.getRegistrationID(getContext()));");

//        mJXLineHashMap.put("\"閣下是否確認打開APP\",", "getString(R.string.confirm_to_open_app)");
    }

}